package com.ithema.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ithema.entity.Article;
import com.ithema.entity.PageBean;
import com.ithema.mapper.ArticleMapper;
import com.ithema.service.ArticleService;
import com.ithema.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Override
    public void insert(Article article) {
        //获取用户id
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("userId");
        article.setCreateUser(id);
        articleMapper.insert(article);
    }

    @Override
    public PageBean<Article> articleList(Integer pageNum,Integer pageSize, Integer categoryId, String state) {
        //获取分页对象
        PageBean<Article> pageBean = new PageBean<>();
        //开启分页查询
        PageHelper.startPage( pageNum, pageSize);
        //获取用户id
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("userId");
        List<Article> list = articleMapper.articleList(id, categoryId, state);
        Page<Article> p= (Page<Article>) list;
        pageBean.setTotal(p.getTotal());
        pageBean.setItems(p.getResult());
        return pageBean;
    }
}
